<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\controller;

use think\Session;
use think\Request;
use think\Cache;
use think\Config;
use think\Db;
use think\Controller;
use \CFormat;
use think\View;

class Supplier extends Controller {
    
    // 视图类实例
    protected $view;
    // Request实例
    protected $request;

    public function __construct()
    {
        parent::__construct();
    
        if (null === $this->view) {
            $this->view = View::instance(Config::get('template'), Config::get('view_replace_str'));
        }
        if (null === $this->request) {
            $this->request = Request::instance();
        }
    }
    
    public function index(){
        $suppliers = Db::name('supplier')->where('status', 1)->select();
        $this->view->assign('suppliers', $suppliers);
        return $this->view->fetch();
    }
    
    public function sulist(){
        $suppliers = Db::name('supplier')->where('status', 1)->select();
        return ding_json(0, 'OK', $suppliers);
    }
    
    public function showOne(){
        $suid = input('param.suid', 0);
        $supplier = Db::name('supplier')->find($suid);
        $this->view->assign('supplier', $supplier);
        return $this->view->fetch();
    }
    
    public function addOrUpdate(){
        $supplier = input('param.', array());
        if($supplier){
            if($supplier['id']){
                Db::name('supplier')->where('id', $supplier['id'])->update($supplier);
            }
            else{
                $supplier['status']=1;
                Db::name('supplier')->insert($supplier);
            }
            return ajax_return_adv('操作成功');
        }
        return ajax_return_adv_error('操作失败');
    }
    
    public function delete(){
        $suid = input('param.suid', 0);
        if(!$suid){
            ajax_return_adv_error('未获取到供应商信息！');
        }
        Db::name('supplier')->where('id', $suid)->update(['status'=>0]);
        return ajax_return_adv('操作成功');
    }
}
